import 'package:flutter/material.dart';

/// 对话框
class ConfirmDialog extends StatelessWidget {
  final String title;
  final String msg;
  final String confirmText;
  final VoidCallback onConfirm;

  ConfirmDialog(
      {super.key,
      required this.title,
      required this.msg,
      required this.onConfirm,
      this.confirmText = '删除'});

  final ButtonStyle confirmStyle = ElevatedButton.styleFrom(
    backgroundColor: Colors.redAccent,
    foregroundColor: Colors.white,
    elevation: 0,
    minimumSize: const Size(70, 35),
    padding: EdgeInsets.zero,
    shape: const StadiumBorder(),
  );

  final ButtonStyle cancelStyle = OutlinedButton.styleFrom(
    elevation: 0,
    minimumSize: const Size(70, 35),
    padding: EdgeInsets.zero,
    shape: const StadiumBorder(),
  );

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.white,
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            _buildTitle(),
            _buildMessage(),
            _buildButtons(context)
          ],
        ),
      ),
    );
  }

  Widget _buildTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Icon(Icons.warning_amber_rounded, color: Colors.orangeAccent),
        const SizedBox(width: 10),
        Text(
          title,
          style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        )
      ],
    );
  }

  Widget _buildMessage() {
    return Padding(
      padding: const EdgeInsets.only(top: 15, bottom: 15),
      child: Text(msg, style: const TextStyle(fontSize: 14)),
    );
  }

  Widget _buildButtons(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        OutlinedButton(
            onPressed: () => Navigator.of(context).pop(),
            style: cancelStyle,
            child: const Text(
              "取消",
              style: TextStyle(fontSize: 14, color: Colors.grey),
            )),
        const SizedBox(width: 10),
        ElevatedButton(
            onPressed: onConfirm,
            style: confirmStyle,
            child: Text(
              confirmText,
              style: const TextStyle(fontSize: 14, color: Colors.white),
            ))
      ],
    );
  }
}
